package com.qingyouchuxing.mall.biz.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.qingyouchuxing.mall.biz.convert.MallMailAddressConvert;
import com.qingyouchuxing.mall.biz.domain.dto.MallMailAddressDTO;
import com.qingyouchuxing.mall.biz.domain.entity.MallMailAddress;
import com.qingyouchuxing.mall.biz.domain.vo.MallMailAddressVO;
import com.qingyouchuxing.mall.biz.mapper.MallMailAddressMapper;
import com.qingyouchuxing.mall.biz.service.IMallMailAddressService;
import com.ruoyi.common.core.exception.ServiceException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MallMailAddressServiceImpl extends ServiceImpl<MallMailAddressMapper, MallMailAddress> implements IMallMailAddressService {
    @Override
    public MallMailAddressVO selectMailAddressById(Long fkTenantId, Long id) {
        return mapper.selectMailAddressById(fkTenantId, id);
    }

    @Override
    public List<MallMailAddressVO> selectMailAddressByIdList(Long fkTenantId, Long id) {
        return mapper.selectMailAddressList(fkTenantId, id);
    }

    @Override
    public boolean addMailAddress(MallMailAddressDTO dto) {
        Long fkTenantId = dto.getFkTenantId();
        String defaultAddress = dto.getDefaultAddress();
        if (defaultAddress.equals("1")) {
            // 查询出是否有默认地址
            MallMailAddressVO defaultAddressInfo = mapper.selectMailAddressInfo(fkTenantId);
            if (ObjectUtil.isNotNull(defaultAddressInfo)) {
                defaultAddressInfo.setDefaultAddress("0");
                MallMailAddress mallMailAddress = MallMailAddressConvert.INSTANCES.votoEntity(defaultAddressInfo);
                mapper.update(mallMailAddress);
            }
        }

        MallMailAddress mallMailAddress = MallMailAddressConvert.INSTANCES.toEntity(dto);
        mallMailAddress.setDelFlag(0);
        return save(mallMailAddress);
    }

    @Override
    public boolean removeMailAddressById(Long fkTenantId, Long id) {
        // 根据收货地址ID查询收货地址信息
        MallMailAddressVO mallMailAddressVO = mapper.selectMailAddressById(fkTenantId, id);
        if (ObjectUtil.isNotNull(mallMailAddressVO)) {
            MallMailAddress mallMailAddressDelete
                = MallMailAddressConvert.INSTANCES.votoEntity(mallMailAddressVO);
            mallMailAddressDelete.setDelFlag(1);
            return removeById(mallMailAddressDelete);


        }
        return false;
    }

    @Override
    public boolean updateMailAddress(MallMailAddressDTO dto) {
        Long fkTenantId = dto.getFkTenantId();
        String defaultAddressNew = dto.getDefaultAddress();
        MallMailAddress mallMailAddress = MallMailAddressConvert.INSTANCES.toEntity(dto);
        mallMailAddress.setDelFlag(0);

        // 根据收货地址ID查询收货地址信息
        MallMailAddressVO mallMailAddressVO = mapper.selectMailAddressById(fkTenantId, dto.getId());
        if (ObjectUtil.isNotNull(mallMailAddressVO)) {
            // 一、
            if (defaultAddressNew.equals("0")) {
                return updateById(mallMailAddress);
            } else {
                // 二、
                String defaultAddressOld = mallMailAddressVO.getDefaultAddress();
                // 2.1
                if (!defaultAddressNew.equals(defaultAddressOld)) {
                    // 查询出是否有默认地址
                    MallMailAddressVO defaultAddressInfo = mapper.selectMailAddressInfo(fkTenantId);
                    if (ObjectUtil.isNotNull(defaultAddressInfo)) {
                        // 将默认地址修改为不默认
                        defaultAddressInfo.setDefaultAddress("0");
                        MallMailAddress mallMailAddressUpdate = MallMailAddressConvert.INSTANCES.votoEntity(defaultAddressInfo);
                        updateById(mallMailAddressUpdate);
                        //
                        return updateById(mallMailAddress);
                    }
                    return updateById(mallMailAddress);
                }
                return updateById(mallMailAddress);
            }

        }
        return false;
    }
}
